package org.wlw.arr;

public class TestSwap {
	public static void main(String[] args) {
		int a = 10;
		int b = 20;
		swap(a,b);
		System.out.println(a+","+b);
		
		A b1 = new A();
		b1.a = 10;
		A b2 = new A();
		b2.a = 20;
		swap(b1,b2);
		System.out.println(b1.a+","+b2.a);
		
		int[] arr1 = {1,2};
		swap(arr1);
		System.out.println(arr1[0]+","+arr1[1]);
	}
	
	public static void swap(int[] arr) {
		int c = arr[0];
		arr[0] = arr[1];
		arr[1] = c;
	}
	
	//swap函数参数中的a和b是基本数据类型，通过值进行传递
	public static void swap(int a,int b) {
		int c = a;
		a = b;
		b = c;
	}
	
	//通过对象传值的，传的是引用（指针），修改参数中对象的值，原有变量会被修改
	public static void swap(A a1,A a2) {
		int c = a1.a;
		a1.a = a2.a;
		a2.a = c;
	}
}

class A {
	int a;
}
